package com.example.yjh.zyfypt_314yjh.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.example.yjh.zyfypt_314yjh.R;
import com.example.yjh.zyfypt_314yjh.adapter.ArticleAdapter;
import com.example.yjh.zyfypt_314yjh.bean.ArticleBean;
import com.example.yjh.zyfypt_314yjh.iface.ArticleListener;
import com.example.yjh.zyfypt_314yjh.model.ArticleModel;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by YJH on 2017/4/9.
 */

public class Fragment1 extends Fragment {
    private View view=null;
    private Context context;
    private int page=1;
    private int lastVisibleItemPosition;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private ArticleAdapter articleAdapter;
    private SharedPreferences sp;
    private String sessionID;
    private List<ArticleBean> list=null;
    ArticleListener articleListener=new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> beanlist) {
            if(page==1)
            {
                list=beanlist;
            }
            else {
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
           articleAdapter.setList(list);//传给adapter
           articleAdapter.notifyDataSetChanged();//通知更新

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context,msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
       //动态加载Fragment1的布局文件
        view=inflater.inflate(R.layout.fragment1,container,false);
        //获取布局上的控件，注意通过view获取
        //注意Toast第一个参数上下文
        //Toast.makeText(context, "加载Fragment微信", Toast.LENGTH_SHORT).show();
        //返回动态生成的view
        sp=context.getSharedPreferences("login",MODE_PRIVATE);
        readSessionID();
        System.out.println("----sessionID="+sessionID);
        initRecycleView();
        ArticleModel articleModel=new ArticleModel();
        articleModel.getResultList("article",page,sessionID,articleListener);
        return view;
    }

    private void readSessionID() {
        sessionID=sp.getString("sessionID",null);
    }

    private void initRecycleView() {

        recyclerView=(RecyclerView)view.findViewById(R.id.recycleview);
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        // list=getData();
        articleAdapter =new ArticleAdapter(context);
        articleAdapter.setList(list);
        recyclerView.setAdapter(articleAdapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    page += 1;
                    //再次实例化ArticleModel，调用方法获取网络数据，请求新一页数据
                    ArticleModel articleModel = new ArticleModel();
                    articleModel.getResultList("article",page,sessionID,articleListener);
                }

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=layoutManager.findLastVisibleItemPosition();
            }
        });

    }

}
